//
//  PGADViewController.m
//  youqu
//
//  Created by 彭鹏 on 16/9/8.
//  Copyright © 2016年 pengpeng. All rights reserved.
//

#import "PGADViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDImageCache.h>
#import "PGTabBarController.h"
#import "PGAdItem.h"

#define PGCode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface PGADViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *placeHoldView;
@property (weak, nonatomic) IBOutlet UIButton *jumpButton;
/**<#name#>*/
@property (nonatomic, weak) NSTimer *timer;

/**<#anotation#>*/
@property (nonatomic, strong) PGAdItem *adItem;

@end

@implementation PGADViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setLaunchImage];
    
    [self loadADData];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1. target:self selector:@selector(updateAdTime) userInfo:nil repeats:YES];
    _timer = timer;
}
- (IBAction)jumpBtnClick {
    [self.timer invalidate];
    PGTabBarController *tabBarController = [[PGTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarController;
    

}

- (void)setLaunchImage {
    UIImage *launchImage = nil;
    if (iphone6p) {
        launchImage = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h"];
    }else if (iphone6) {
        launchImage = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }else if (iphone5) {
        launchImage = [UIImage imageNamed:@"LaunchImage-568h"];
    }else if (iphone4) {
        launchImage = [UIImage imageNamed:@"LaunchImage"];
    }
    self.launchImageView.image = launchImage;
    
}

- (void)loadADData {
//    UITableViewCell
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = PGCode2;
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        NSArray *arr = responseObject[@"ad"];
        
        PGAdItem *adItem = [PGAdItem mj_objectWithKeyValues:[arr firstObject]];
        self.adItem = adItem;

        UIImageView *adImageView = [[UIImageView alloc] init];
        [adImageView sd_setImageWithURL:[NSURL URLWithString:adItem.w_picurl]];
        adImageView.userInteractionEnabled = YES;
        NSInteger adh = SCREENW/adItem.w * adItem.h;
        
        if (adh > SCREENH*0.82) {
            adh = SCREENH*0.82;
        }
        
        adImageView.frame = CGRectMake(0, 0, SCREENW, adh);
        [self.placeHoldView addSubview:adImageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adImageViewClick)];
        [adImageView addGestureRecognizer:tap];

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

- (void)updateAdTime {
    static NSInteger time = 3;
    int ddd;
    [self.jumpButton setTitle:[NSString stringWithFormat:@"跳过( %zd )",time] forState:UIControlStateNormal];
    time--;
//    if (time == 3) {
        [self jumpBtnClick];
//    }
}

- (void)adImageViewClick {
    NSLog(@"%s, line = %d", __FUNCTION__,__LINE__);
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:self.adItem.w_picurl]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.adItem.w_picurl]];
    }
    
    
    
    
}


- (BOOL)prefersStatusBarHidden {
    return YES;
}


@end
